package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.com.utt.common.mapper.UserMapper;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.UserRecord;
import cn.com.utt.unms.service.SmsMarketService;
import cn.com.utt.unms.service.UserRecordService;
import cn.com.utt.unms.vo.SmsMarketingInfo;
import cn.com.utt.unms.vo.SmsMarketingInput;
@RestController
@RequestMapping("v1/smsMarketing")
public class SmsMarketingController {
	@Autowired
	SmsMarketService smsMarketService;
	@Autowired
	UserRecordService userRecordService;
	@Autowired
	UserMapper userMapper;
	
	/**
	 * 短信营销列表
	 * @param principal
	 * @param input
	 * @return
	 */
	@RequestMapping(value = "/smsMarketingList", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> smsMarketingList(Principal principal,@RequestBody TableInput input){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<SmsMarketingInfo> output=smsMarketService.querySmsMarketingList(input, user.getId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("list", output.getList());
		return result.toJsonObject();
	}
	/**
	 * 新建-初始化
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/loadSmsMarketing", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> loadSmsMarketing(Principal principal){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result=smsMarketService.loadSmsMarketing(user.getId(),user.getGroupId());
		return result.toJsonObject();
	}
	/**
	 * 新增
	 * @param principal
	 * @param userids  最近访问用户id
	 * @param smsMarketing
	 * @return
	 */
	@RequestMapping(value = "/creatSmsMarketing", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> creatSmsMarketing(Principal principal,@RequestBody SmsMarketingInput smsMarketingInput){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result=smsMarketService.addSmsMarketing(smsMarketingInput.getUserids(),smsMarketingInput.getSmsMarketing(),user.getId());
		return result.toJsonObject();
	}
	/**
	 * 自定义-用户列表
	 * @param principal
	 * @param input
	 * @return
	 */
	@RequestMapping(value = "/loadUserDefineList", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> userDefineList(Principal principal,@RequestBody TableInput input){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<UserRecord> output=smsMarketService.queryRecentUserByCondition(input, user.getId(), user.getGroupId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal()); 
		result.put("list", output.getList());
		return result.toJsonObject();
	}
	
	/**
	 * 更新短信-标题或者内容
	 * @param principal
	 * @param title
	 * @param content
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/editSmsInfo", method = { RequestMethod.POST })
	public Map<String, Object> editSmsInfo(Principal principal,String title,String content,Long id){
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		if(content.trim().length()<=0||content.trim().equals("")){
			result.setErrorCode(1);
			result.setErrorMsg("短信内容不能为空");
			return result.toJsonObject();
		}else if(content.indexOf('【')==-1 && content.indexOf('】')==-1){
			result.setErrorCode(1);
			result.setErrorMsg("短信内容必须添加签名，例如【艾泰】");
			return result.toJsonObject();
		}
		result=smsMarketService.editSmsInfo(title,content,id,user.getId());
		return result.toJsonObject();
	}
	/**
	 * 删除
	 * @param principal
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteSmsInfo", method = { RequestMethod.POST })
	public  Map<String, Object> deleteSmsInfo(Principal principal,@RequestParam(value = "ids[]") List<Long> ids){
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result=smsMarketService.delSmsInfo(ids,user);
		return result.toJsonObject();
	}

}
